package whr.modules.interview.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import whr.common.utils.PageUtils;
import whr.common.utils.R;
import whr.common.utils.ResponseApi;
import whr.modules.interview.entity.InterviewRecordsEntity;
import whr.modules.interview.entity.dto.CheckResultDto;
import whr.modules.interview.entity.dto.InterviewResultDto;
import whr.modules.interview.service.InterviewRecordsService;
import whr.modules.interview.service.StationService;
import whr.modules.sys.controller.AbstractController;
import whr.modules.sys.dao.SysOrganizationsMapper;
import whr.modules.sys.dao.SysUserDao;
import whr.modules.sys.entity.SysUserEntity;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author youkun
 * @email youkun@gmail.com
 * @date 2024-09-25 10:46:31
 */
@RestController
@RequestMapping("interview/interviewRecords")
@Slf4j
public class InterviewRecordsController extends AbstractController {

    @Autowired
    private InterviewRecordsService interviewRecordsService;

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private StationService stationService;

    @Autowired
    private SysOrganizationsMapper sysOrganizationsMapper;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("interview:interviewRecords:list")
    public R list(@RequestParam Map<String, Object> params){
        // 1.获取当前登录用户的Id
        Long userId = getUserId();
        // 2.把当前登录用户的Id 封装到params参数中
        params.put("userId", userId);

        PageUtils page = interviewRecordsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("interview:interviewRecords:info")
    public R info(@PathVariable("id") Integer id){
		InterviewRecordsEntity interviewRecords = interviewRecordsService.getById(id);

        // 封装面试官的姓名
//        interviewRecords.setInterviewer(sysUserDao.queryInterviewer(Long.valueOf(interviewRecords.getInterviewer())));

        // 封装应聘岗位的名称
        interviewRecords.setStation(stationService.getStationName(interviewRecords.getStation()));

        // 封装入职部门的名称
        // 判断入职部门是否为空
        if (interviewRecords.getEntryOrganization() != null) {
            interviewRecords.setEntryOrganization(sysOrganizationsMapper.getOrganizationName(Integer.valueOf(interviewRecords.getEntryOrganization())));
        }

        return R.ok().put("interviewRecords", interviewRecords);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("interview:interviewRecords:save")
    public R save(@RequestBody InterviewRecordsEntity interviewRecords){

        // 1.获取当前登录用户的信息
        Long userId = getUserId();

        // 2.保存当前登录用户的信息
        interviewRecords.setCreateUser(userId.toString());
        interviewRecords.setCreateTime(new Date());

        // 3.保存当前的面试记录信息为 等待录入结果状态
        interviewRecords.setFlowStatus(0);

        interviewRecordsService.save(interviewRecords);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("interview:interviewRecords:update")
    public R update(@RequestBody InterviewRecordsEntity interviewRecords){

        // 1.通过前端传的面试官的名称 找到相应的面试官Id
//        SysUserEntity sysUserEntity = sysUserDao.queryByUserName(interviewRecords.getInterviewer());
//        interviewRecords.setInterviewer(sysUserEntity.getUserId().toString());

        // 2.通过前端传的应聘岗位的名称 找到相应的岗位Id
        String stationCode = stationService.getStationCode(interviewRecords.getStation());
        interviewRecords.setStation(stationCode);

        interviewRecordsService.updateById(interviewRecords);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("interview:interviewRecords:delete")
    public R delete(@RequestBody Integer[] ids){
		interviewRecordsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /*
    * 录入面试结果的接口
    * 只能面试官录入
    * */
    @PostMapping("/addInterviewResult")
    @RequiresPermissions("interview:interviewRecords:addInterviewResult")
    public R addInterviewResult(@RequestBody InterviewResultDto interviewResultDto){
        log.info("面试结果录入数据:{}",interviewResultDto);
        // InterviewResultDto(interviewId=1, signIn=1, signInTime=Fri Sep 27 14:52:29 CST 2024, minPay=2500, interviewScore=75, interviewStatus=1)

        interviewRecordsService.addInterviewResult(interviewResultDto);
        return R.ok("录入面试结果成功");
    }

    /*
    * 录取审核接口
    * 只能由人事主管或者高层操作
    * */
    @PostMapping("/checkResult")
    @RequiresPermissions("interview:interviewRecords:checkResult")
    public R checkResult(@RequestBody CheckResultDto checkResultDto){
        log.info("录取审核的信息为:{}",checkResultDto);
        // CheckResultDto(interviewId=2, enrollStatus=1, enrollTime=Fri Sep 27 16:16:53 CST 2024, entryStatus=1, entryTime=Fri Sep 27 16:16:57 CST 2024, entryOrganization=1, notEmployReason=null)

        // 根据前端传回的入职部门名称 查询入职部门的id
        if (checkResultDto.getEntryOrganization() != null){
            Integer organizationId = sysOrganizationsMapper.getOrganizationId(checkResultDto.getEntryOrganization());
            checkResultDto.setEntryOrganization(organizationId.toString());
           // checkResultDto.setEntryOrganization(checkResultDto.getEntryOrganization());
        }

        interviewRecordsService.checkResult(checkResultDto);

        return R.ok("审核完成");
    }

    /*
    * 获取历史面试记录
    * */
    @RequestMapping("/getHistory")
    public R getHistory(@RequestParam Map<String, Object> params){
        // 获取历史面试记录的参数为:{t=1727671064314, name=小小, phone=13333333331, createTime=2024-09-27 11:05:22}
        log.info("获取历史面试记录的参数为:{}",params);

        InterviewRecordsEntity interviewRecordsEntity = interviewRecordsService.getHistory(params);

        return R.ok().put("history",interviewRecordsEntity);
    }

    /*
    * 统计面试者的评分分布
    * */
    @RequestMapping("/getScoreCount")
    public R getScoreCount(){
        // 直接查询整表
        List<Integer> scoreCount = interviewRecordsService.getScoreCount();
        return R.ok().put("scoreCount",scoreCount);
    }


    /*
    * 获取近七天的新增面试
    * */
    @RequestMapping("/getTurnover")
    public R getTurnover(){

        return R.ok().put("turnover",interviewRecordsService.getTurnover());
    }

    //查询待面试的人数
    @RequestMapping("/getWaitingCount")
    public ResponseApi getWaitingCount(){
        return ResponseApi.success(interviewRecordsService.getWaitingCount());
    }



}
